layui.use(['table', 'admin', 'ax', 'func','tree','ztree','layer','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    //var admin = layui.admin;
    var func = layui.func;
    var tree = layui.tree;
    var $ZTree = layui.ztree;
    var layer = layui.layer;
    var form = layui.form;

    /**
     * 网上建店信息gcg_wsjd管理
     */
    var GcgWsjd = {
        tableId: "gcgWsjdTable"
    };

    /**
     * 初始化表格的列
     */
    GcgWsjd.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'gcgWsjdId', hide: true, title: ''},
            {field: 'yjzt', hide: true, title: ''},
            {field: 'sjr', hide: true, title: ''},
            {field: 'gcgWsjdYinhangkaId', hide: true, title: ''},
            {align: 'center', type: "numbers", title: '序号'},
             {field: 'gcgProjectId', hide: true, title: ''},
            // {field: 'yaoma', sort: true, title: '妖码'},
            {field: 'mingcheng',title: '门店名称(分店名称)',width: 180,templet: function (d) {
                    let meituan = '';
                    if (d.yjzt != "" && d.yjzt != undefined) {
                        meituan = "<span style='background-color: #1e9fff; padding: 0px 5px'></span>";
                    }
                    return `
                        <div style="height: 56px">${d.mingcheng}
                        <br>
                        <span>${meituan}</span>
                        </div>
                          
                    
                    `
                    // return '<div>'+d.mingcheng+'</div>' + d.tm.length > 1 ? '<span><sss></sss></span>' : '' + '<span style="color: #999">妖码：</span><span style="color: #999">'+d.yaoma+'</span>';
                }},
            // {field: 'dizhiSheng', sort: true, title: '地址(省)'},
            // {field: 'dizhiShengView', sort: true, title: '地址(省)名称'},
            // {field: 'dizhiShi', sort: true, title: '地址(市)'},
            // {field: 'dizhiShiView', sort: true, title: '地址(市)名称'},
            // {field: 'dizhiXianqu', sort: true, title: '地址(县区)'},
            // {field: 'dizhiXianquView', sort: true, title: '地址(县区)名称'},
           // {field: 'dizhiXiangxi', title: '详细地址'},
            {field: 'address', title: '详细地址'},
            {field: 'pinpai',title: '对外门店名称(品牌)'},
            {field: 'guishu',title: '公司归属'},
            {field: 'mdTel', title: '门店电话'},
            // {field: 'lxrName', sort: true, title: '门店联系人'},
            // {field: 'lxrTel', sort: true, title: '门店联系人电话'},
            // {field: 'jsTel', sort: true, title: '接受门店手机号码'},
            // {field: 'pinlei', sort: true, title: '菜系品类'},
             {field: 'mdYysj',title: '营业时间',width: 110,},
            // {field: 'isTcw', sort: true, title: '是否有停车位'},
            // {field: 'tcwPrice', sort: true, title: '停车位价格'},
            // {field: 'renjunPrice', sort: true, title: '人均价格'},
            {field: 'farenXingming',title: '法人',width: 75,align: "center"},
            {field: 'yhkName',align: 'center',title: '开户人',width: 90,templet: function (d) {
                    if (d.yhkName === undefined || d.yhkName === "") {
                        return '<a class="layui-btn layui-btn-xs btn-green" lay-event="manager" id="manager">选择开户人</a>';
                    } else {
                        return '<a class="layui-btn layui-btn-xs btn-green" lay-event="manager" id="manager">'+ d.yhkName +'</a>';
                    }


                }},
            {field: 'yhkHao', title: '银行卡',width: 80},
            // {field: 'isFapiao', sort: true, title: '是否支持开发票'},
            // {field: 'isYouhui', sort: true, title: '是否申请优惠'},
            // {field: 'farenSfzPic1', sort: true, title: '法人身份证人像面'},
            // {field: 'farenSfzPic2', sort: true, title: '法人身份证国徽面'},
            // {field: 'farenSfzPic3', sort: true, title: '法人手持身份证'},
            // {field: 'dzSfzPic1', sort: true, title: '店长身份证人像面'},
            // {field: 'dzSfzPic2', sort: true, title: '店长身份证国徽面'},
            // {field: 'dzSfzPic3', sort: true, title: '店长手持身份证'},
            // {field: 'yingyeZhizhao', sort: true, title: '营业执照'},
            // {field: 'shipinXkz', sort: true, title: '食品经营许可证'},
            // {field: 'zfbShouquan', sort: true, title: '支付宝授权函'},
            // {field: 'mentouPics', sort: true, title: '门头照'},
            // {field: 'neijingPics', sort: true, title: '内景照'},
            {field: 'shStatus',title: '状态',templet:'#statusTpl2',width:75,align: "center"},
            {field: 'createTime',title: '申请时间',align: "center"},
            //{field: 'createUser', sort: true, title: 'create_user 创建人'},
            {field: 'shenpiShijian',title: '通过(驳回)时间',align: "center"},
            {field: 'miaoshu',title: '驳回原因',align: "center",width: 100},
            //{field: 'updateUser', sort: true, title: 'update_user 修改人'},
            {align: 'center', toolbar: '#tableBar', title: '操作',minWidth: 100,align: "center"}
        ]];
    };
    var status = ''
    form.on('select(status)', function (data) {
        status = data.value
        GcgWsjd.search();
    });
    form.on('select(mingcheng)', function (data) {
        GcgWsjd.search();
    });

    /**
     * 点击查询按钮
     */
    GcgWsjd.search = function () {
        var queryData = {};

        queryData['yaoma'] = $('#yaoma').val();
        queryData['mingcheng'] = $('#mingcheng').val();
        queryData['shStatus'] =$("#status  option:selected").val();

        table.reload(GcgWsjd.tableId, {
            where: queryData, page: {curr: 1}
        });
    };
    /**
     * 弹出添加对话框
     */
    // GcgWsjd.openAddDlg = function () {
    //     func.open({
    //         title: '添加网上建店信息gcg_wsjd',
    //         content: Feng.ctxPath + '/gcgWsjd/add',
    //         tableId: GcgWsjd.tableId
    //     });
    // };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      GcgWsjd.openEditDlg = function (data) {
          func.open({
              title: '驳回美团团购',
              content: Feng.ctxPath + '/gcgWsjd/edit?gcgWsjdId=' + data.gcgWsjdId,
              tableId: GcgWsjd.tableId
          });
      };

    /**
     * 查看邮件记录
     */
    GcgWsjd.email = function (data) {
        func.open({
            title: '邮件记录',
            content: Feng.ctxPath + '/gcgWsjd/email?gcgWsjdId=' + data.gcgWsjdId,
            tableId: GcgWsjd.tableId,
            height: 350,
            width:'500px',
        });
    };


    /**
     * 导出excel按钮
     */
    GcgWsjd.exportExcel = function () {
        var checkRows = table.checkStatus(GcgWsjd.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    GcgWsjd.sendEmail = function () {
        // func.open({
        //     title: '发送邮件',
        //         content: ' <div style="text-align:center;" id="div">\n' +
        //             ' <span class="span" style="line-height: 150px;">' + msg + '</span>\n' +
        //             ' <textarea type="text" hidden class="input" id="reason" placeholder="请输入邮件地址" style="height: 100px;width: 92%;margin-top: 10px;padding-left: 5px;"></textarea>\n' +
        //             ' </div>',
        //     tableId: GcgWsjd.tableId
        // });

        var checkRows = table.checkStatus(GcgWsjd.tableId);
        var data = checkRows.data;
        var ids = "";
        if (checkRows.data.length === 0) {
            Feng.error("请选择要发送邮件的数据");
        } else {
            for (var i in data) {
                var yhkName=data[i].yhkName;
                var gcgWsjdYinhangkaId=data[i].gcgWsjdYinhangkaId;
                var num=parseInt(i)+1;
                if(yhkName == ""|gcgWsjdYinhangkaId==""){
                    alert("第"+num+"行请先设置开户人才能发送邮件!");
                    return false;
                }
                var state=data[i].shStatus;
                if(state!="3"){
                    alert("审核通过之后才能发送邮件!");
                    return false;
                }
            }
            //页面层-自定义
            var index = layer.open({
                type: 1,
                title: false,
                async: false,
                area: ['260px','190px'],
                closeBtn: 0,
                btnAlign: 'c',
                btn: ['确定', '取消'],
                content: '<textarea id="email" lay-verify="required" required  class="layui-input" type="text" autocomplete="off"\n' +
                    'placeholder="请输入邮件地址" style="height: 120px;padding-top: 10px;width: 245px;margin-left: 7px;\n' +
                    '    margin-top: 12px;" >luziwen@meituan.com</textarea>',
                yes: function () {
                    var email= $('#email').val();
                    if(email==""){
                        alert("请输入邮件地址!");
                        return false;
                    }else{
                        for (var i in data) {
                            ids += data[i].gcgWsjdId + ",";
                        }
                        // var dm = $("#dmpx").val();
                        // var timeLimit = $("#timeLimitpx").val();
                        //获取导出Excel的数据
                        // var url = Feng.ctxPath + "/ckpx/expExcel?ids=" +ids+"&timeLimit="+timeLimit;
                        var url = Feng.ctxPath + "/gcgWsjd/expExcel?ids="+ids+"&email="+email;
                        location.href = url;
                        layer.closeAll();
                    }

                    // layer.closeAll();
                    // ajax.set("ids", ids);
                    // ajax.set("bhyy", bhyy);
                    // ajax.start();
                },
            });
            layer.closeAll(index);


            // for (var i in data) {
            //     ids += data[i].gcgWsjdId + ",";
            // }
            // // var dm = $("#dmpx").val();
            // // var timeLimit = $("#timeLimitpx").val();
            // //获取导出Excel的数据
            // // var url = Feng.ctxPath + "/ckpx/expExcel?ids=" +ids+"&timeLimit="+timeLimit;
            // var url = Feng.ctxPath + "/gcgWsjd/expExcel?ids="+ids;
            // location.href = url;
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    GcgWsjd.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/gcgWsjd/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(GcgWsjd.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("gcgWsjdId", data.gcgWsjdId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    var _delay = false;
    GcgWsjd.tg = function (data) {
        var operation = function () {
            if(_delay) {
                return;
            };
            _delay = true;
            // setTimeout(() =>{
            //     _delay = false
            // },2000);
            var ajax = new $ax(Feng.ctxPath + "/gcgWsjd/confirmOk", function (data) {
                Feng.success("通过成功!");
                table.reload(GcgWsjd.tableId);
            }, function (data) {
                Feng.error("通过失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.gcgWsjdId);
            ajax.start();
        };
        Feng.confirm("是否通过?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + GcgWsjd.tableId,
        url: Feng.ctxPath + '/gcgWsjd/list',
        page: true,
        height: "full-100",
        cellMinWidth: 100,
        cols: GcgWsjd.initColumn(),
        done: function (res, curr, count) {
            $('.title-tr').remove()
            for (let i =0;i<res.data.length;i++){
                if(res.data[i].shStatus== 1){
                    $("[data-field='miaoshu']").css('display','none');
                    $('.laytable-cell-1-0-8').css('width','200px')

                } else if(res.data[i].shStatus== 3){
                    $("[data-field='miaoshu']").css('display','none');
                    $('.laytable-cell-1-0-8').css('width','200px')
                }
            }
            if (status == 9){
                $("[data-field='miaoshu']").css('display','');
                $('.laytable-cell-1-0-8').css('width','120px')
            }

            $(".laytable-cell-1-0-11").css('overflow','visible');

            $('.layui-table thead tr').before('<tr class="title-tr"><td></td><th colspan="7" style="text-align: center;" class="jbth"><span class="jb">基本信息</span></th><th colspan="3" style="text-align: center;">金融信息</th><th colspan="5" class="th"></th></tr>')
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        GcgWsjd.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    GcgWsjd.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        GcgWsjd.exportExcel();
    });

    $('#btnSend').click(function () {
        GcgWsjd.sendEmail();
    });

    // 工具条点击事件
    table.on('tool(' + GcgWsjd.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'manager') {
            GcgWsjd.zhiDing(data);
        }

        if (layEvent === 'bh') {
            GcgWsjd.openEditDlg(data);
        } else if (layEvent === 'delete') {
            GcgWsjd.onDeleteItem(data);
        }else if (layEvent === 'tg') {
            GcgWsjd.tg(data);
        }else if (layEvent === 'email') {
            GcgWsjd.email(data);
        }
    });

    /**
     * 指定
     */
    GcgWsjd.zhiDing = function (data) {
        var gcgWsjdId=data.gcgWsjdId;
        var gcgWsjdYinhangkaId=data.gcgWsjdYinhangkaId;
        let url = "/gcgWsjd/saveKhr?gcgWsjdId"+gcgWsjdId;
        let index = layer.open({
            area: ['400px', '400px'],
            title: '指定',
            btn: ['确定', '取消'],
            btnAlign: 'c',
            type: 2,
            async: false,
            content: "/gcgWsjd/zdkhr?gcgWsjdYinhangkaId="+gcgWsjdYinhangkaId,
            yes: function (index, layero) {
                //var arr = $(layero).find("iframe")[0].contentWindow.back();
                var gcgWsjdYinhangkaId = $(layero).find("iframe")[0].contentWindow.backTel();
                var ajax = new $ax(url, function (msg) {
                    Feng.success("修改成功！");
                    layer.close(index);
                    table.reload(GcgWsjd.tableId);
                    // parent.location.reload();
                }, function (data) {
                    alert("修改失败！" + data.responseJSON.message);
                    // layer.alert("修改失败！" + data.responslaypageeJSON.message,{ icon: 5, title: "提示", offset: "auto", skin: 'layui-layer-molv' });

                    // Feng.error("修改失败！" + data.responseJSON.message);
                });
                // ajax.set(type, arr);
                ajax.set("gcgWsjdYinhangkaId", gcgWsjdYinhangkaId);
                ajax.set("gcgWsjdId", gcgWsjdId);
                // ajax.set("taskId", taskId);
                ajax.start();
            }
        });
    };
});
